﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Xml;

namespace Pasteles
{
    public partial class Add_Cake : Form
    {
        string sConfig_hostname;
        string sConfig_dbname;
        string sConfig_dbusername;
        string sConfig_dbpassword;
        string sConfig_SerialPort;
        string sConfig_Baudrate;
        string sConfig_Parity;
        string sConfig_Datalength;
        
        
        string sConfig_line1 = "";
        string sConfig_line2 = "";
        string sConfig_line3 = "";
        string sConfig_line4 = "";
        string sConfig_line5 = "";
        MySqlConnection connection;

        public Add_Cake()
        {
            InitializeComponent();
            readConfigurationFile();
            preparingMySQL();
            
        }

        private void preparingMySQL()
        {

            string MyConString = "SERVER=" + sConfig_hostname + ";" +
                "DATABASE=" + sConfig_dbname + ";" +
                "UID=" + sConfig_dbusername + ";" +
                "PASSWORD=" + sConfig_dbpassword + ";";
            connection = new MySqlConnection(MyConString);
            //MySqlCommand command = connection.CreateCommand();
            //MySqlDataReader Reader;
            //connection
            try
            {
                connection.Open();
                // MessageBox.Show("Connect to MySQL OK!");

            }
            catch (Exception error)
            {
                MessageBox.Show("Connection Error " + error.ToString());
            }
            connection.Close();
        }

        private void readConfigurationFile()
        {
            XmlReader reader = XmlReader.Create("config.xml");
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element
                    && reader.Name == "Pasteles")
                {
                    /*MessageBox.Show("hostname= " + reader.GetAttribute(0));
                    MessageBox.Show("dbname= " + reader.GetAttribute(1));
                    MessageBox.Show("dbusername= " + reader.GetAttribute(2));
                    MessageBox.Show("dbpassword= " + reader.GetAttribute(3));*/
                    sConfig_hostname = reader.GetAttribute(0);
                    sConfig_dbname = reader.GetAttribute(1);
                    sConfig_dbusername = reader.GetAttribute(2);
                    sConfig_dbpassword = reader.GetAttribute(3);

                    while (reader.NodeType != XmlNodeType.EndElement)
                    {
                        reader.Read();
                        if (reader.Name == "Printing")
                        {
                            /*MessageBox.Show("SerialPort= " + reader.GetAttribute(0));
                            MessageBox.Show("Baudrate= " + reader.GetAttribute(1));
                            MessageBox.Show("Parity= " + reader.GetAttribute(2));
                            MessageBox.Show("Datalength= " + reader.GetAttribute(3));
                            MessageBox.Show("Stopbits= " + reader.GetAttribute(4));*/
                            sConfig_SerialPort = reader.GetAttribute(0);
                            sConfig_Baudrate = reader.GetAttribute(1);
                            sConfig_Parity = reader.GetAttribute(2);
                            sConfig_Datalength = reader.GetAttribute(3);
                            //sConfig_Stopbits=reader.GetAttribute(4);

                            while (reader.NodeType != XmlNodeType.EndElement)
                            {
                                reader.Read();
                                if (reader.Name == "line1")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line1 = " + reader.Value);
                                            sConfig_line1 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if

                                if (reader.Name == "line2")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line2 = " + reader.Value);
                                            sConfig_line2 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if
                                if (reader.Name == "line3")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line3 = " + reader.Value);
                                            sConfig_line3 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if
                                if (reader.Name == "line4")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line4 = " + reader.Value);
                                            sConfig_line4 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if
                                if (reader.Name == "line5")
                                {
                                    while (reader.NodeType != XmlNodeType.EndElement)
                                    {
                                        reader.Read();
                                        if (reader.NodeType == XmlNodeType.Text)
                                        {
                                            //MessageBox.Show("line5 = " + reader.Value);
                                            sConfig_line5 = reader.Value;
                                        }
                                    }
                                    reader.Read();
                                } //end if
                            }
                        } //end if
                    } //end while
                } //end if
            } //end while
            reader.Close();
        }
        private bool nonNumberEntered = false;//bien flag dung de khoa ban phim nhap lieu
        private void validateNum(KeyEventArgs e)//Ham chi cho phep nhap so nguyen duong
        {
            nonNumberEntered = false;
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    if (e.KeyCode != Keys.Back)
                    {

                        nonNumberEntered = true;
                    }
                }
            }
            if (Control.ModifierKeys == Keys.Shift)
            {
                nonNumberEntered = true;
            }
        }

        private void Add_Cake_Load(object sender, EventArgs e)
        {
            //MessageBox.Show(ModifyCake.idstatic.ToString());
            if (ModifyCake.idstatic > -1)
            {
                try
                {

                    connection.Open();
                    MySqlDataAdapter myDA = new MySqlDataAdapter();

                    string sQuery = "select TEN_BANH,HAN_SU_DUNG,GIA_KINH_DOANH,GIA_SAN_XUAT,SALE_OFF,CACH_LAM from banh where MA_BANH=" + ModifyCake.idstatic;

                    myDA.SelectCommand = new MySqlCommand(sQuery, connection);
                    var dataReader = myDA.SelectCommand.ExecuteReader();
                    dataReader.Read();
                    tb_Name.Text = dataReader.GetString("TEN_BANH");

                    tb_ExpiryDate.Text = dataReader.GetString("HAN_SU_DUNG");
                    tb_ProducingPrice.Text = dataReader.GetString("GIA_SAN_XUAT");
                    tb_SellingPrice.Text = dataReader.GetString("GIA_KINH_DOANH");
                    tb_SaleOff.Text = dataReader.GetString("SALE_OFF");
                    tb_Recipe.Text = dataReader.GetString("CACH_LAM");
                }
                catch (Exception en)
                {

                }
            }
            else 
            {
            }
            connection.Close();
        }

        private void tb_SellingPrice_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb_ProducingPrice_TextChanged(object sender, EventArgs e)
        {

        }

        private void tb_SaleOff_TextChanged(object sender, EventArgs e)
        {

        }

        private void btn_Save_Click(object sender, EventArgs e)

        {


            if (ModifyCake.idstatic == -1)
            {
                if (tb_Name.Text == "" || tb_ExpiryDate.Text == "" || tb_SellingPrice.Text == "" || tb_SaleOff.Text=="")
                {
                    MessageBox.Show("Không được bỏ trống dữ liệu nhập");
                }
                else
                {
                    connection.Open();
                    string temp = "";
                    if (tb_Name.TextLength >= 16)
                    {
                        temp = tb_Name.Text.Remove(15);
                    }
                    else
                        temp = tb_Name.Text;
                    MySqlDataAdapter myDA = new MySqlDataAdapter();
                    string sQuery = "call them_banh(" + ModifyCake.maloaibanh + ",'" + tb_Name.Text + "','" + temp + "'," + Convert.ToInt16(tb_ExpiryDate.Text) + ","
                    + tb_SellingPrice.Text + "," + tb_ProducingPrice.Text + "," + tb_SaleOff.Text + ",'" + tb_Recipe.Text + "')";

                    myDA.SelectCommand = new MySqlCommand(sQuery, connection);
                    myDA.SelectCommand.ExecuteReader();

                    connection.Close();

                    this.Close();
                }
            }
            else if (ModifyCake.idstatic != -1 && ((tb_Name.Text == "") || (tb_SellingPrice.Text == "") || (tb_ExpiryDate.Text == "") || (tb_SaleOff.Text == "")))
            {
                MessageBox.Show("Please add data to TextBox");
            }
            else
            {
                if (tb_Name.Text == "" || tb_ExpiryDate.Text == "" || tb_SellingPrice.Text == "" || tb_SaleOff.Text == "")
                {
                    MessageBox.Show("Không được bỏ trống dữ liệu nhập");
                }
                else
                {

                    connection.Open();
                    string temp = "";
                    if (tb_Name.TextLength >= 16)
                    {
                        temp = tb_Name.Text.Remove(15);
                    }
                    else
                        temp = tb_Name.Text;
                    MySqlDataAdapter myDA = new MySqlDataAdapter();
                    string sQuery = "call sua_banh(" + ModifyCake.idstatic + "," + ModifyCake.maloaibanh + ",'" + tb_Name.Text + "','" + temp + "'," + tb_ExpiryDate.Text + ","
                    + tb_SellingPrice.Text + "," + tb_ProducingPrice.Text + "," + tb_SaleOff.Text + ",'" + tb_Recipe.Text + "')";

                    myDA.SelectCommand = new MySqlCommand(sQuery, connection);
                    myDA.SelectCommand.ExecuteReader();
                    connection.Close();
                    this.Close();
                }
            }
            
        }

        private void tb_SellingPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (nonNumberEntered == true)
            {
                e.Handled = true;
            }
        }

        private void tb_ProducingPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (nonNumberEntered == true)
            {
                e.Handled = true;
            }
        }

        private void tb_SellingPrice_KeyDown(object sender, KeyEventArgs e)
        {
            this.validateNum(e);
        }

        private void tb_ProducingPrice_KeyDown(object sender, KeyEventArgs e)
        {
            this.validateNum(e);
        }

        private void tb_ExpiryDate_KeyDown(object sender, KeyEventArgs e)
        {
            this.validateNum(e);
        }

        private void tb_ExpiryDate_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (nonNumberEntered == true)
            {
                e.Handled = true;
            }
        }

        private void tb_SaleOff_KeyDown(object sender, KeyEventArgs e)
        {
            this.validateNum(e);
        }

        private void tb_SaleOff_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (nonNumberEntered == true)
            {
                e.Handled = true;
            }
        }

        
        
        
    }
}
